welcome to the module to deal with missing values in this module you are going to learn what are the strategies to deal with missing values and how to detect them how to report them and either you can choose to ignore them or you can find the way with your best knowledge you may have to fill them so that you will not lose any information let's first go to the setup again for the missing value you will need our numpy as np and pandas as pd you will also need the dataset which have already uploaded here as the economy of US with na information I purposely reduced the economy of us dataset and made a lot of missing values in there so that we can learn how to deal with those missing values again you can either drop the file associated in this module to this file panel by click this file on the left part or you can either click this upload and then choose where you saved that CSV file then you are able to find the economy_of_us_na.CSV you are going to click on the 3 dots copy the path and paste it into here and then you will have the setup ready and then we can first go with how we can learn and how we can detect how we can have the knowledge of the missing values so this is our dataframe which is quite quite as simple as I said I purposely reduced it just for this tutorial so we have only 13 observations and for the year we can clearly see we have one missing value for the GTP nominal as we can see here we have 1 2 3 and 4 missing values for the GTP growth we have 7 missing values of course it is quite straightforward if we just count in this 13 observation how about if we have 13,000 of observation can you continue to count one by one? actually we cannot as we learned the data mining and data analysis is actually we have to deal with massive amount of data rather than just 13 observations so in order to know that how many missing values we have for this certain column we actually have to rely on some functionality for example isnull() isnull() is a built in functionality you can call directly from any data frame and then you will immediately see that if this value is missing like NaN you are going to find out that cell is filled with True if the value is not missing you will see that the value is filled with False because we know for the False it will be 0 for the True it will be 1 which means that we can use the sum() as a function to summarize all these True and False and the result represents what is the number of the missing values in this particular column so we just simply go for a for loop for each of the column we are going to make a sum of this isnull() boolean value and for each of the column we are going to print out this message and as we can see this message is using the format the first placeholder is for the c which is the name of the column and the second placeholder is for the miss which is the number of missing value we calculated by isnull() and the sum() the results we can see here is 1 missing value 4 missing value and 7 missing value which are aligning with what we just count by hand however this function simply a for loop with the isnull() and sum() and the print the message out can be really handy if you have a lot of attributes and a lot of observations the most simple way to deal with the missing value is actually dropping them or in other words ignore them for example if we just dropna() that is another function showing that you are going to ignore or eliminate any observation has the missing value then you are going to get a new data frame, called the df2 df2 as you can see here has all the rows and from the index we can see because the index is the label we are missing a lot of labels previously we have the label 0-12 that are 13 observations but now we have only 6 of the observations and it's just 1/2 of our dataset and that is only for 3 columns if we have more than 100 columns even one column may have only 1% of missing value you will end up a really really small dataset that is why dropping may not be always the choice you can rely on it depends so as a comparison if we dropna() from df we are going to get a new data frame called df2 and the df itself as we learned in order to protect you from accidentally dropping or deleting information then the df will keep the same which means that if you dropna() the df will still be the same without the change if you really really want to make the effect in the original data frame you can make the optional argument like inplace = True if you specify inplace = True basically you are confirming you know the consequence of doing this to your original data frame and then the data frame itself will be affected you can try by your own let's continue with our dropna() and you can specify the direction or you can specify how you are going to dropna by default the axis will be 0 basically we dropna() based on a row because we know the row most of time represents observations so because we have axis = 0 by default when we call dropna() only the rows with a NaN will be dropped if you specify axis = 1 basically you are going to drop any column has a missing value looking into our small sample actually year GDP and GDP growth all have more or less missing value that will result in an empty data frame that has nothing on it only the index the other very useful argument you can feed to dropna is the thresh that is you are going to set up a threshold that how many of value that are not missing then you are going to keep them for example if here you are going to dropna() you are going to keep those rows has at least 3 values then basically you have to have all the value for the columns that means that it is the same as you dropna() because the maximum value you can have is 3 so let's try with a smaller threshold as 2 if we are going to do threshold as equal to 2 basically means that for these 3 columns if we have 2 columns that are not having any missing value then you are going to keep it for example we are going to have this one because year and GDP nominal is there and this GDP growth is missing even though this row has a missing value but we have 2 columns that have values they are not missing so they satisfy this threshold so the dropna() will not consider this row as the one we are going to drop if you even have the threshold as equal to 1 basically as long as we have one value on one column then we are going to keep the entire observation such as this one for this year 1983 we are missing the GDP and also missing the GDP growth we are still going to keep them is because we have this thresh equal to 1 and then how about we have the axis equal to 1 which means that we are going to drop the column but we can set up the threshold that means that for this column if you satisfy you have 7 observations that has no missing value you are going to keep that column in this example we can see 2 columns satisfy this requirement for the year we have only one missing value basically means that we have 11 or 12 value there that is more than 7 for the GDP nominal we are missing 4, so we have 9 values in GDP nominal so we're going to keep them however for the GDP growth since we have missing value as 7, which means that we have only 6 then we are not going to keep it if we have the threshold higher, as 10 basically you are going to eliminate more columns because the threshold higher means that you are going to require more for that column to be left in your elimination and the results will be we have only one column left as the year so those are the strategies we can do with dropping or we can eliminating the rows or columns with missing value and sometimes when you have too many attributes even with a small percentage of missing value you will end up a very small size of data by just dropping them so that will not be applicable or not practical if we want to have more data because data means insight if that is the case we have to find a way to use our best knowledge to fill that missing data so here we are going to learn several strategies fill the missing value with a constant fill the missing value with forward or backward fill fill the missing value with mean or with mode and then at the end we're going to do a summary for how these different strategy can show the difference of the missing value let's continue with filling with constant so for example you can simply fill in the value with 'NA' as a string we just need to call fillna() and fill in the value you are going to have it filled if there is a missing value so we know that NaN means missing but 'NA' can be used as a flag so that you can notice this is a observation has 3 missing values this is an observation has 2 missing values it is not necessarily to be the NaN because NaN can be recognized by numpy and by pandas and 'NA' can be recognized by you as a human and also we can do different fillings for example, 	for the year, which is in numbers we can actually feel the year field as a new column so that we can do a straightforward comparison which we have the NaN as missing and now we filled with a 'YEAR' as something we may pay more attention to later on this missing value as a constant can be a string can be an integer can be a float can be anything you want it to be a placeholder or a replacer for that missing value but you have to provide enough justification why you fill this missing value for sure another way which is more dynamic that is forward and a backward fill if you have your data set in a time series for example weather for example stock price the value if you have a day missing then you can rely on the day of previous and the day of the later to get an estimation what will be the possible value for that missing value so for example in here we did a fillna() and the method as equal to 'ffill' that is forward fill we can show you the difference between the existing missing values right here and the value filled as you can see forward field means that we are going to borrow the value of the previous one this previous one may be the previous day maybe the previous year maybe the previous observation anyway just a previous record then we have the new value fill in as the one in previous for this NaN missing value we are using the previous one forward here and for this missing value we are using this previous one forward fill here so on and so forth the backfill is similar rather than use the information of the yesterday we are going to use the information of tomorrow so for the back fill, we are going to use one detail points later so we can see this missing value is filled with this value not this one if we use this one this is forward fill if we use this one it is backward fill so we have all this NaN missing value by the later day or later year or the later observation that is the backfill this is particularly useful for the weather as I said for the stock price for any time series analysis it will be really really handy if you don't have that advantage if you have no dependence between the previous and the later observation for the missing value then you still can try to use some statistic understanding of that column for example mean and mode we Learned that mean is the average and we can get the mean by calling a column under the mean() function so if you feel an a with the meaning basically you are going to fill in the best knowledge you have for this missing value which is the average of all the value in this column that might be a safe guess if you want to do and then you can see that all the missing value by this by this way will be the same as the 4400 something that will be the mean value basically for the mode it will be similar but for the mean we just use the average and for the mode we use the most typical or the most frequent observation of your data so to get the mode we learned we can use the mode function because mode() returns a list of the frequency of your observation so we are going to use the first element on that list which represents the most frequent observation of your data so fill in this mode will also result in fill in the same number again again again and again however this mode is already very typical that is the one we can observe the most of the time in your data set however you have to be careful with the mode I purposely invaded it here because in our dataset when we look at our value here this 2857.3 is actually as normal as all the other observations because it is all normal or counted as 1 the mode simply were ordered based on their index number that is why we get this 2857.3 as our mode but you can see clearly in this demo the mode is not a good choice simply because we do not have the theoretically most frequent observation as a summary I prepared all the filling methods with a 'NA" with a constant a 0 with the forward and backward as method with the mean and mode as our constant etc for the GDP growth because we just played with GDP nominal and here I have all the columns listed which one this one is the original one with the missing value this one filled with 'NA' this one filled with 0 this one is forward this one is backward this one is mean, and this one is m mode I believe this comparison can give you a lot of insights and also make you clear what each of the field and a is actually doing I hope you can enjoy this summary and then I will see you in next module